<template>
	<view @touchmove.stop.prevent>
		<view class="mask" @touchmove.stop.prevent="moveHandle" @click="isShowModal=true"></view>
		<view class="container">
			<view class="tip-pay"><text @click="isShowModal=true">取消</text><text>支付</text></view>
			<view class="moneytitle">￥<text>{{bond/100}}</text></view>
			<view class="moneyInfo">服务内容</view>
			<view class="moneyInfobar">
				<text>
					1.当月生效，截止至本月月底
					2.金牌购买不可退还
				</text>
			</view>
			<view class="payTapyTitle">支付方式</view>

			<view class="choosePay">
				<view class="payItem" @click="handleSelect(2)">
					<image src="http://images.linglinggong.net/icon1/yue.png" mode="aspectFill"></image><text>余额<text
							class="yueText">￥{{ userInfo.amount/100 }}</text></text>
					<view :class="requestObj.payType==2 ? 'selected' : 'select'"></view>
				</view>
				<!--  #ifdef MP-ALIPAY || MP-TOUTIAO -->
				<view class="payItem" @click="handleSelect(0)">
					<image src="http://images.linglinggong.net/icon1/alipay.png" mode="aspectFill"></image>
					<text>支付宝</text>
					<view :class="requestObj.payType==0 ? 'selected' : 'select'"></view>
				</view>
				<!-- #endif -->
		
				<view class="payItem" @click="handleSelect(3)">
					<image src="http://images.linglinggong.net/icon1/wechat.png" mode="aspectFill"></image>
					<text>微信</text>
					<view :class="requestObj.payType==3 ? 'selected' : 'select'"></view>
				</view>
			</view>

			<showModalButton v-if="isShowModal" :modalText1="'距离金牌只差一步了,'" :modalText2="'确认要取消吗？'"
				:buttonLeftText="'暂时放弃'" :buttonRightText="'继续支付'" :modalTextSize="'36rpx'" :modalTextHeight="'53rpx'"
				:rightButtonColor="'#FE4B28'" @clickLeft="closePayPage(false),isShowModal=false"
				@clickRight="isShowModal=false"></showModalButton>

			<view class="confirm" @click="handlePayBond">确认支付</view>

			<!-- 支付密码弹框 -->
			<view v-if="isOpenModal" class="share-modal">
				<view class="main" :class="{'errorpw':isShowPassWordAnimation}">
					<image @click="handleCloseModal" class="close" src="http://images.linglinggong.net/icon1/47.png">
					</image>
					<view class="modal-title">
						<text>请输入钱包支付密码</text>
					</view>
					<view class="pay-code">
						<input type="number" :focus="focus" v-model="requestObj.payPwd" maxlength="6"
							@input="getPayCode" />
						<block v-for="(item, index) in ranges" :key="index">
							<view :class="['box', { active: codeIndex === item}]">
								<view class="line"></view>
								<text>{{ codeArr[index] ? codeArr[index] : ''}}</text>
								<!-- <text v-if="!isShowCode" style="transform: translateY(10%);">{{ codeArr[index] ? '*' : ''}}</text> -->
							</view>
						</block>
					</view>
					<view class="repwd" v-if="isShowForgetPassword" @click="toRePasssWord">忘记密码</view>
					<!-- <view class="button" @click="handleComfirmPay">确认</view> -->
				</view>
			</view>

		</view>
	</view>
</template>

<script>
	import showModalButton from '@/components/showModalButton/showModalButton.vue'
	export default {
		props: {
			getTaskId: {
				type: Number,
				default: 1
			}
		},
		components: {
			showModalButton,
		},
		data() {
			return {
				focus: false, //键盘输入焦点
				codeIndex: 1,
				codeArr: [], //密码
				ranges: [1, 2, 3, 4, 5, 6],
				isOpenModal: false, //是否打开分享弹框
				requestObj: {
					payPwd: '',
					payType: 0, //0支付宝，1微信，2钱包  //taskId cardId
					// #ifdef MP-WEIXIN
					payType: 3, //0支付宝，1微信，2钱包  //taskId cardId
					//#endif
				},
				userInfo: {
					user: {},
					params: {}
				}, //当前用户信息
				bond: '', //每人诚意金
				isShowForgetPassword: false, //是否弹出忘记密码
				isShowPassWordAnimation: false, //密码错误执行动画
				isShowModal: false, //是否显示对话框
				modalStytle: 99,
				middleContext: '距离金牌只差一步了，确认要取消吗？',
				contentColor: '#000000',
				leftContext: '暂时放弃',
				rightContext: '继续支付',
			};
		},
		methods: {
			//关掉页面
			closePayPage(flag) {
				this.$emit('closePayPage', flag);
			},
			// 得到输入的密码
			getPayCode(e) {
				let {
					value
				} = e.detail
				let del = false
				this.requestObj.payPwd = value
				if (value.length < this.codeArr.length) {
					del = true
				}
				this.codeArr.length = value.length
				this.codeArr.fill("●")
				this.codeIndex = this.codeArr.length + 1
				if (del) {
					return
				}
				this.codeArr[value.length - 1] = value[value.length - 1]
				setTimeout(() => {
					if (this.codeArr.length != 0) {
						this.$set(this.codeArr, [this.codeArr.length - 1], "●")
					}

				}, 1000)
				if (this.codeIndex == 7) {
					this.handleComfirmPay()
				}
				console.log('codeArr', this.codeIndex, this.codeArr)
			},
			//点击链接至账户安全。
			toRePasssWord() {
				uni.navigateTo({
					url: '/pages/account-security/account-security'
				})
			},
			// 钱包支付确认
			async handleComfirmPay() {
				console.log('钱包支付确认', this.requestObj)
				let res = await this.$fetch(this.$api.pay_gold_medal, this.requestObj, 'POST')
				console.log('钱包支付确认', res)
				if (res.code) {
					this.emptyInput()
					uni.showToast({
						title: res.msg,
						icon: 'none'
					})
					return
				} else {
					this.isOpenModal = false
					this.handleCloseModal()
					uni.showToast({
						title: '购买成功~',
						icon: 'none'
					})
					setTimeout(() => {
						this.closePayPage(true)
					}, 800)
				}
			},
			//清空输入框，更新是否弹出忘记密码标识
			emptyInput() {
				this.isShowPassWordAnimation = true
				this.isShowForgetPassword = true
				this.requestObj.payPwd = ''
				this.codeArr = []
				this.codeIndex = 1
				setTimeout(() => {
					this.isShowPassWordAnimation = false
				}, 1200)
			},
			// 关闭分享弹框
			handleCloseModal() {
				console.log('关闭')
				this.isOpenModal = false
				this.requestObj.payPwd = ''
				this.codeArr = []
				this.codeIndex = 1
			},
			//选择提现类型
			handleSelect(index) {
				this.requestObj.payType = index
				if (index == 2) {
					if (uni.getStorageSync('isHavePayPassword') == false) {
						this.setPayPassword()
					}
				}
			},
			// 确认发布，
			async handlePayBond() {
				let _this = this
				//支付宝支付
				if (this.requestObj.payType == 0) {
					let res = await this.$fetch(this.$api.pay_gold_medal, this.requestObj, 'POST')
					if (res.code) return uni.showToast({
						title: res.msg,
						icon: 'none'
					})
					
					uni.requestPayment({
						provider: 'alipay',
						orderInfo: res.msg, //微信、支付宝订单数据
						success: function(res) {
							console.log('success:' + JSON.stringify(res));
							uni.showToast({
								title: '购买成功~',
								icon: 'none'
							})
							setTimeout(() => {
								_this.closePayPage(true)
							}, 800)
						},
						fail: function(err) {
							console.log('fail:' + JSON.stringify(err));
						}
					})
				} else if (this.requestObj.payType == 2) {
					if (uni.getStorageSync('isHavePayPassword') == false) {
						this.setPayPassword()
					} else {
						this.isOpenModal = true
						this.focus = true
					}
				} else {
					//微信支付
					this.requestObj.paybegtype='wx'
					
					let res = await this.$fetch(this.$api.pay_gold_medal, this.requestObj, 'POST')
					console.log(99999,res)
					if (res.code) return uni.showToast({
						title: res.msg,
						icon: 'none'
					})
					const info = res.data
					
					// #ifdef MP-WEIXIN
					this.MP_WXPay(info)
					// #endif
				}
			},
			/* 微信小程序支付 */
			MP_WXPay(info) {
				
				uni.requestPayment({
					provider: 'wxpay',
					timeStamp: info.timeStamp, //时间戳
					nonceStr: info.nonceStr,
					package: info.package,
					signType: info.signType,
					paySign: info.paySign,
					success: res => {
						console.log('success:' + JSON.stringify(res));
						uni.showToast({
							title: '购买成功~',
							icon: 'none'
						})
						setTimeout(() => {
							this.closePayPage(true)
						}, 800)
					},
					fail: err => {
						console.log('fail:' + JSON.stringify(err));
					}
				})
			},
			// 获取数据配置
			async getBond() {
				let res4 = await this.$fetch(this.$api.property_list_by_type, {
					type: 1
				}, 'POST')
				//console.log(res4.data.bondS)
				this.bond = res4.data.Gold_medal_amount
			},

			setPayPassword() {
				uni.showModal({
					title: '您还未设置支付密码',
					content: '是否现在就去设置支付密码？',
					success: function(res) {
						if (res.confirm) {
							uni.navigateTo({
								url: '/pages/set-pay-code/set-pay-code'
							})
						} else if (res.cancel) {
							uni.showToast({
								title: '未设置支付密码~',
								icon: 'none'
							})
						}
					}
				})
			},
			// 获取当前用户信息，及服务标识
			async getUserInfo() {
				let res = await this.$fetch(this.$api.get_current_user, {}, 'GET')
				//	console.log(res)
				if (res.code) return uni.showToast({
					title: res.msg,
					icon: 'none'
				})
				//console.log(res.data)
				uni.setStorageSync('userInfo', res.data)
				uni.setStorageSync('userType', res.data.userType)
				uni.setStorageSync("level", res.data.user.level) //用户等级
				this.userInfo = res.data
				// if (this.userInfo.amount != 0) {
				// 	this.requestObj.payType = 2
				// }
			},

		},
		beforeMount() {
			this.getUserInfo()
			this.getBond()
			// if(uni.getStorageSync('isHavePayPassword') == true) this.requestObj.payType = 2
		},
		onBackPress(e) {
			this.isShowModal = true
			return true
		},
	}
</script>

<style lang="less" scoped>
	.container {
		position: fixed;
		bottom: 0;
		background: #F2F2F2;
		border-radius: 25rpx 25rpx 0 0;
		overflow: hidden;
		display: flex;
		flex-direction: column;
		align-items: center;
		z-index: 502;

		&>.share-modal {
			z-index: 11;
			position: fixed;
			bottom: 0;
			height: 100vh;
			width: 100vw;
			background-color: rgba(0, 0, 0, 0.40);
			display: flex;
			align-items: center;
			justify-content: center;

			&>.main {
				// border: 2rpx solid #000000;
				position: relative;
				top: -100rpx;
				width: 570rpx;
				height: 250rpx;
				padding: 30rpx;
				background: #FFFFFF;
				box-shadow: 0 0 0 0 rgba(0, 0, 0, 0.05) inset;
				border-radius: 17px;
				line-height: 40rpx;

				display: flex;
				flex-direction: column;
				align-items: center;

				&>.modal-title {
					font-size: 17px;
					margin: 30rpx 0 50rpx;

					&>.imgs {
						position: relative;

						image {
							position: absolute;
							top: 0rpx;
							bottom: 0;
							right: -80rpx;
							width: 50rpx;
							height: 40rpx;
						}
					}
				}

				&>.pay-code {
					// border: 2rpx solid #000000;
					padding-bottom: 10rpx;
					display: flex;
					align-items: center;
					justify-content: center;
					text-align: center;
					flex-wrap: wrap;
					position: relative;

					&>input {
						position: absolute;
						background: transparent;
						top: 0;
						left: -100%;
						width: 200%;
						height: 100%;
						text-align: left;
						z-index: 9;
						opacity: 1;

						// border: 1px solid #000000;
					}

					&>.box {
						display: flex;
						align-items: center;
						justify-content: center;
						position: relative;
						width: 80rpx;
						height: 80rpx;
						margin-right: 10rpx;
						font-size: 22px;
						line-height: 90rpx;
						box-sizing: border-box;
						background-color: #F2F2F2;
						border-radius: 2px;

						&:last-child {
							margin-right: 0;
						}
					}

					&>.active {
						border: 2rpx solid #cccccc;

						@keyframes twinkling {
							0% {
								opacity: 0.1;
							}

							50% {
								opacity: 0.8;
							}

							100% {
								opacity: 0.1;
							}
						}

						&>.line {
							display: block;
							position: absolute;
							left: 50%;
							top: 50%;
							transform: translate(-50%, -50%);
							width: 2upx;
							height: 40upx;
							background: #333333;
							animation: twinkling 1s infinite ease;
						}
					}
				}

				.repwd {
					height: 40rpx;
					margin-bottom: -40rpx;
					font-size: 24rpx;
					color: #FE4B28;
				}

				&>.button {
					display: flex;
					align-items: center;
					justify-content: center;

					width: 180rpx;
					height: 70rpx;
					margin-top: 60rpx;
					border-radius: 50rpx;
					background-color: #FCC928;

					&:active {
						opacity: 0.7;
					}
				}

				&>.close {
					position: absolute;
					right: 30rpx;
					top: 30rpx;
					width: 42rpx;
					height: 42rpx;
				}
			}

		}

		&>.tip-pay {
			width: 690rpx;
			height: 50rpx;
			margin-top: 20rpx;
			display: flex;

			&>text:nth-child(1) {
				font-size: 32rpx;
				font-weight: 500;
				color: #ABABAB;
				line-height: 45rpx;
			}

			&>text:nth-child(2) {
				font-size: 36rpx;
				font-weight: bold;
				color: #000000;
				line-height: 45rpx;
				margin-left: calc(50% - 56rpx);
				transform: translate(-50%);
			}
		}

		&>.main {
			background-color: #FFFFFF;
			display: flex;
			flex-direction: column;
			padding: 30upx;
			margin-top: 30rpx;

		}

		&>.confirm {
			position: fixed;
			bottom: 73rpx;
			background: #FCC928;
			display: flex;
			justify-content: center;
			align-items: center;
			width: 690rpx;
			height: 88rpx;
			font-size: 36rpx;
			color: #402110;
			border-radius: 12rpx;

			&:active {
				opacity: 0.7;
			}
		}
	}

	.errorpw {
		animation: swing 1s .15s linear 1;
	}

	@keyframes swing {

		10%,
		90% {
			transform: translate(-4rpx, 0);
		}

		20%,
		80% {
			transform: translate(+4rpx, 0);
		}

		30%,
		70% {
			transform: translate(-6rpx, 0);
		}

		40%,
		60% {
			transform: translate(+6rpx, 0);
		}

		50% {
			transform: translate(-8rpx, 0);
		}
	}

	.mask {
		z-index: 501;
		width: 100vw;
		height: 100vh;
		position: fixed;
		top: 0;
		background: rgba(0, 0, 0, 0.40);
	}

	.moneytitle {
		width: 750rpx;
		height: 80rpx;
		margin-top: 60rpx;
		display: flex;
		justify-content: center;
		align-items: flex-end;
		font-size: 36rpx;
		font-weight: bold;
		color: #FE4B28;
		line-height: 45rpx;

		&>text {
			font-size: 80rpx;
			font-weight: bold;
			color: #FE4B28;
			line-height: 80rpx;
		}
	}

	.moneyInfo {
		font-size: 28rpx;
		font-weight: bold;
		color: #000000;
		line-height: 45rpx;
		margin-top: 20rpx;
		width: 690rpx;
	}

	.moneyInfoPs {
		font-size: 24rpx;
		font-weight: 500;
		color: #A5A5A5;
		line-height: 24rpx;
		margin-top: 14rpx;
		width: 690rpx;
	}

	.moneyInfobar {
		width: 690rpx;
		height: 106rpx;
		background: #FFFFFF;
		border-radius: 8rpx;
		margin-top: 26rpx;
		display: flex;
		justify-content: space-between;
		align-items: center;
		padding: 36rpx 0;

		&>text:nth-child(1) {
			font-size: 26rpx;
			font-weight: 500;
			color: #000000;
			line-height: 58rpx;
			margin-left: 30rpx;
		}

		&>text:nth-child(2) {
			font-size: 26rpx;
			font-weight: 500;
			color: #FE4B28;
			line-height: 58rpx;
			margin-right: 30rpx;
		}
	}

	.payTapyTitle {
		width: 690rpx;
		font-size: 28rpx;
		font-weight: bold;
		color: #000000;
		line-height: 45rpx;
		margin-top: 40rpx;
		margin-bottom: 30rpx;
	}

	.choosePay {
		width: 690rpx;
		height: 418rpx;
		background: #FFFFFF;
		border-radius: 8rpx;
		margin-bottom: 212rpx;
		display: flex;
		flex-direction: column;
		align-items: center;
	}

	.payItem {
		width: 630rpx;
		height: 60rpx;
		margin: 40rpx 0;
		display: flex;
		align-items: center;
		justify-content: space-between;

		&>image {
			width: 60rpx;
			height: 60rpx;
		}

		&>text {
			font-size: 26rpx;
			font-weight: 500;
			color: #000000;
			line-height: 36rpx;
			margin-left: 20rpx;
			flex: 1;
		}

		.yueText {
			font-size: 26rpx;
			font-weight: 500;
			color: #A5A5A5;
			line-height: 45rpx;
			margin-left: 20rpx;
		}

		.select {
			width: 44rpx;
			height: 44rpx;
			border: 1px solid #AFAFAF;
			border-radius: 50%;
		}

		.selected {
			width: 45rpx;
			height: 45rpx;
			background-image: url(http://images.linglinggong.net/icon1/hdg.png);
			background-repeat: repeat;
			background-size: 100% 100%;
		}
	}
</style>
